import pygame
import random
import math
from typing import List, Tuple

class Particle:
    def __init__(self, x: float, y: float, color: Tuple[int, int, int], 
                 velocity: Tuple[float, float], lifetime: float, size: int = 3):
        self.x = x
        self.y = y
        self.color = color
        self.velocity = velocity
        self.lifetime = lifetime
        self.age = 0
        self.size = size
        self.alpha = 255
    
    def update(self, dt: float) -> bool:
        """更新粒子状态，返回是否存活"""
        self.x += self.velocity[0] * dt
        self.y += self.velocity[1] * dt
        self.age += dt
        
        # 计算透明度
        self.alpha = int(255 * (1 - self.age / self.lifetime))
        
        return self.age < self.lifetime
    
    def draw(self, screen: pygame.Surface):
        """绘制粒子"""
        particle_surface = pygame.Surface((self.size * 2, self.size * 2), pygame.SRCALPHA)
        pygame.draw.circle(particle_surface, (*self.color, self.alpha), 
                         (self.size, self.size), self.size)
        screen.blit(particle_surface, (int(self.x - self.size), int(self.y - self.size)))

class ParticleSystem:
    def __init__(self):
        self.particles: List[Particle] = []
    
    def create_explosion(self, x: int, y: int, color: Tuple[int, int, int], 
                        particle_count: int = 20):
        """创建爆炸效果"""
        for _ in range(particle_count):
            angle = random.uniform(0, math.pi * 2)
            speed = random.uniform(100, 200)
            velocity = (math.cos(angle) * speed, math.sin(angle) * speed)
            lifetime = random.uniform(0.5, 1.0)
            size = random.randint(2, 4)
            
            self.particles.append(Particle(x, y, color, velocity, lifetime, size))
    
    def create_trail(self, x: int, y: int, color: Tuple[int, int, int],
                    direction: Tuple[float, float], particle_count: int = 5):
        """创建拖尾效果"""
        for _ in range(particle_count):
            offset_x = random.uniform(-5, 5)
            offset_y = random.uniform(-5, 5)
            velocity = (direction[0] * random.uniform(0.5, 1.5),
                      direction[1] * random.uniform(0.5, 1.5))
            lifetime = random.uniform(0.2, 0.5)
            
            self.particles.append(Particle(x + offset_x, y + offset_y, 
                                        color, velocity, lifetime))
    
    def create_heal_effect(self, x: int, y: int, amount: int = 20):
        """创建治疗效果的粒子"""
        for _ in range(amount):
            angle = random.uniform(0, math.pi)  # 只在上半圈产生粒子
            speed = random.uniform(50, 100)
            velocity = (math.cos(angle) * speed, -math.sin(angle) * speed)
            lifetime = random.uniform(0.8, 1.2)
            size = random.randint(3, 5)
            
            self.particles.append(Particle(x, y, (0, 255, 0), velocity, lifetime, size))
    
    def create_shield_effect(self, x: int, y: int, radius: int = 50):
        """创建护盾效果的粒子"""
        for _ in range(15):
            angle = random.uniform(0, math.pi * 2)
            # 粒子沿圆形轨迹运动
            pos_x = x + math.cos(angle) * radius
            pos_y = y + math.sin(angle) * radius
            
            # 粒子速度垂直于圆的半径
            velocity = (-math.sin(angle) * 50, math.cos(angle) * 50)
            self.particles.append(Particle(pos_x, pos_y, (0, 100, 255), 
                                        velocity, 1.0, 4))
    
    def create_buff_effect(self, x: int, y: int, color: Tuple[int, int, int]):
        """创建增益效果的粒子"""
        for _ in range(10):
            angle = random.uniform(0, math.pi * 2)
            speed = random.uniform(30, 60)
            velocity = (math.cos(angle) * speed, math.sin(angle) * speed)
            lifetime = random.uniform(0.5, 1.0)
            
            self.particles.append(Particle(x, y, color, velocity, lifetime))
    
    def create_debuff_effect(self, x: int, y: int, color: Tuple[int, int, int]):
        """创建减益效果的粒子"""
        for _ in range(10):
            velocity = (random.uniform(-30, 30), random.uniform(50, 100))
            lifetime = random.uniform(0.5, 1.0)
            
            self.particles.append(Particle(x, y, color, velocity, lifetime))
    
    def create_card_trail(self, x: int, y: int, dx: float, dy: float, color: Tuple[int, int, int], count: int = 20):
        """创建卡牌轨迹粒子效果"""
        for _ in range(count):
            # 添加随机偏移
            offset_x = random.randint(-10, 10)
            offset_y = random.randint(-10, 10)
            
            # 计算速度
            speed = random.uniform(0.5, 2.0)
            angle = math.atan2(dy, dx)
            particle_dx = math.cos(angle) * speed + random.uniform(-1, 1)
            particle_dy = math.sin(angle) * speed + random.uniform(-1, 1)
            
            # 创建粒子
            particle = Particle(
                x + offset_x,
                y + offset_y,
                color,
                (particle_dx, particle_dy),
                lifetime=random.uniform(0.5, 1.0),
                size=random.randint(2, 4)
            )
            self.particles.append(particle)
    
    def update(self, dt: float):
        """更新所有粒子"""
        self.particles = [p for p in self.particles if p.update(dt)]
    
    def draw(self, screen: pygame.Surface):
        """绘制所有粒子"""
        for particle in self.particles:
            particle.draw(screen) 